// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.miginfocom.swing.MigLayout;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.UIManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.presenter.Presenter;


public class TreatmentRecordStaffPanel extends ApplicationPanel {
	private JList apptsList;
	private JTextField patientField;
	private JTextField doctorField;
	private JTextField nurseField;
	private JTextField textField_1;
	private JTextField txtDoctorsOrders;
	private JTextField vitalField;
	private JTextField diagnosisField;
	private JLabel lblStatus;
	JSONArray appts;
	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.action."
				+ "ActionHandler");
	}
	public TreatmentRecordStaffPanel() {
		
		setBackground(SystemColor.inactiveCaptionBorder);
		setLayout(null);
		
		JPanel topHalf = new JPanel();
		topHalf.setBackground(SystemColor.inactiveCaptionBorder);
		topHalf.setBounds(0, 0, 584, 195);
		add(topHalf);
		topHalf.setLayout(null);
		
		JPanel toolbar = new JPanel();
		toolbar.setBackground(SystemColor.inactiveCaptionBorder);
		FlowLayout fl_toolbar = (FlowLayout) toolbar.getLayout();
		fl_toolbar.setAlignment(FlowLayout.LEFT);
		toolbar.setBounds(10, 11, 564, 33);
		topHalf.add(toolbar);
		
		JLabel lblPleaseSelectA = new JLabel("Please select an appointment to view");
		toolbar.add(lblPleaseSelectA);
		
		
		JButton btnNew = new JButton("Create New Treatment Record");
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Presenter.getInstance().createTreatmentRecord(patientField.getText(), doctorField.getText(),
						nurseField.getText(), txtDoctorsOrders.getText(), textField_1.getText(),
						vitalField.getText(), diagnosisField.getText());
			}
		});
		toolbar.add(btnNew);
		
		JScrollPane tablePanel = new JScrollPane();
		tablePanel.setBounds(10, 48, 564, 136);
		topHalf.add(tablePanel);
		
		apptsList = new JList();
		tablePanel.setViewportView(apptsList);
		
		JLabel lblPleaseSelctA = new JLabel("Treatment Record Info:");
		lblPleaseSelctA.setBounds(10, 193, 366, 14);
		add(lblPleaseSelctA);
		
		patientField = new JTextField("Patient Name");
		patientField.setBounds(10, 206, 166, 20);
		add(patientField);
		patientField.setColumns(10);
		
		doctorField = new JTextField();
		doctorField.setText("Doctor");
		doctorField.setBounds(10, 237, 166, 20);
		add(doctorField);
		doctorField.setColumns(10);
		
		nurseField = new JTextField();
		nurseField.setText("Attending Nurse");
		nurseField.setBounds(10, 268, 166, 20);
		add(nurseField);
		nurseField.setColumns(10);
		
		textField_1 = new JTextField("Symptoms");
		textField_1.setBounds(10, 299, 166, 80);
		add(textField_1);
		textField_1.setColumns(10);
		
		txtDoctorsOrders = new JTextField();
		txtDoctorsOrders.setText("Doctors Orders");
		txtDoctorsOrders.setBounds(186, 299, 381, 80);
		add(txtDoctorsOrders);
		txtDoctorsOrders.setColumns(10);
		
		vitalField = new JTextField();
		vitalField.setText("Vital Signs");
		vitalField.setBounds(186, 206, 264, 20);
		add(vitalField);
		vitalField.setColumns(10);
		
		diagnosisField = new JTextField();
		diagnosisField.setText("Diagnosis");
		diagnosisField.setBounds(186, 237, 264, 47);
		add(diagnosisField);
		diagnosisField.setColumns(10);
}	

	@Override
	public void show(JSONObject response)
	{
		try 
		{
			String action = response.getString("action");
			JSONArray times = response.getJSONArray("appts");
			Object[] data = new Object[times.length()];

			
			for (int i = 0; i < times.length(); i++) 
			{
				// TODO Put this into a more readable format
				JSONObject appt = times.getJSONObject(i);
				JSONObject spec =  appt.getJSONObject("spec");
				long dateIndex = spec.getLong("date");
				data[i] = new Date(dateIndex).toString();
			}
			apptsList.setListData(data);
			patientField.setText("");
			doctorField.setText("");
			nurseField.setText("");
			vitalField.setText("");
			diagnosisField.setText("");
			lblStatus.setText("");
			if (action.equals("read_user")) 
			{
				JSONObject user = response.getJSONObject("user");
				LOG.info("Showing user " + user.getString("username"));

				patientField.setText(user.getString("username"));
				doctorField.setText(user.getString("type"));
			} 
			else if(action.equals("update_user")) 
			{
				LOG.info("User updated");
				
				lblStatus.setText(patientField.getText() + "'s account updated.");
			} 
			else 
			{
				LOG.warning("Panel shown for unexpected action");
			}
		}	
		catch(JSONException e) 
		{
			LOG.log(Level.SEVERE, "", e);
		}
	}
		
}
